package com.example.demo.log;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.SysLog;
import com.example.demo.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author zhanpengguo
 * @date 2019-07-09 14:12
 */
@Slf4j
@Component
public class MyLogListener {

    @Autowired
    LogService logService;

    @Async
    @EventListener(MyLogEvent.class)
    public void saveLog(MyLogEvent myLogEvent){
        SysLog sysLog = (SysLog) myLogEvent.getSource();
        log.info("监听到有事件：{}", JSONObject.toJSONString(sysLog));
        logService.saveLog(sysLog);
    }
}
